//
//  StatesListViewModel.swift
//  weibo
//
//  Created by JonsonLiu on 2017/3/3.
//  Copyright © 2017年 JonsonLiu. All rights reserved.
//

import UIKit

class StatesListViewModel: NSObject {

    var state: [StatesViewModel]?



    func loadData(isPullUP:Bool, finised: @escaping (_ isSuccess: Bool) -> ()) {
        var max_id = 0
        
        
        if isPullUP {
           max_id =  state?.last?.states?.id ?? 0
            
        }

        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
        let param = [
            "access_token": "2.00VbzG3CbZxgeBafd89365faJgnK_B",
            "max_id":"\(max_id)"
        ]
        NetworkTools.shardTools.request(method: .Get, urlString: url, parameters: param as AnyObject?) { (responseObject, error) in

            if error != nil {

                print(error!)
                return
            }
            
           

            guard let resDict = responseObject as? [String: AnyObject]else {

                return
            }

            guard let dicts = resDict["statuses"] as? [[String: AnyObject]] else {
                return
            }


//            print("这里是最后的数组 \(dicts)")
            var stateArray = [StatesViewModel]()

            for value in dicts {
//                print(value)
//                print(value["pic_urls"])
                let state = HTStates(dict: value)
                let model = StatesViewModel()
                model.states = state
                stateArray.append(model)



            }
//            print(stateArray)
//            self.state = stateArray
           
            if self.state == nil {
                self.state = [StatesViewModel]()
            }
            
            if isPullUP{
                self.state =    stateArray + self.state!
            }else{
                self.state = self.state! + stateArray
            }


            finised(true)
//            self.tableView.reloadData()
        }
    }


}
